<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>商家注册 - 新商城</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100">
    <header class="bg-white shadow-md">
        <div class="container mx-auto px-4 py-6">
            <div class="text-2xl font-bold text-blue-600">新商城</div>
        </div>
    </header>

    <main class="container mx-auto px-4 py-12">
        <div class="max-w-md mx-auto bg-white rounded-lg shadow-lg p-8">
            <div class="text-center mb-6">
                <h1 class="text-2xl font-bold">商家注册</h1>
                <p class="text-gray-600">请填写以下信息完成商家注册</p>
            </div>

            <form id="registerForm" class="space-y-4">
                <div>
                    <label for="username" class="block text-gray-700 mb-2">用户名</label>
                    <div class="relative">
                        <span class="absolute inset-y-0 left-0 flex items-center pl-3 text-gray-500">
                            <i class="fa fa-user"></i>
                        </span>
                        <input type="text" id="username" name="username" 
                               class="w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
                               required>
                    </div>
                </div>

                <div>
                    <label for="password" class="block text-gray-700 mb-2">密码</label>
                    <div class="relative">
                        <span class="absolute inset-y-0 left-0 flex items-center pl-3 text-gray-500">
                            <i class="fa fa-lock"></i>
                        </span>
                        <input type="password" id="password" name="password" 
                               class="w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
                               required>
                    </div>
                </div>

                <div>
                    <label for="shopname" class="block text-gray-700 mb-2">店铺名称</label>
                    <div class="relative">
                        <span class="absolute inset-y-0 left-0 flex items-center pl-3 text-gray-500">
                            <i class="fa fa-store"></i>
                        </span>
                        <input type="text" id="shopname" name="shopname" 
                               class="w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
                               required>
                    </div>
                </div>

                <div>
                    <label for="contactPerson" class="block text-gray-700 mb-2">联系人</label>
                    <div class="relative">
                        <span class="absolute inset-y-0 left-0 flex items-center pl-3 text-gray-500">
                            <i class="fa fa-user-circle"></i>
                        </span>
                        <input type="text" id="contactPerson" name="contactPerson" 
                               class="w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
                               required>
                    </div>
                </div>

                <div>
                    <label for="phone" class="block text-gray-700 mb-2">联系电话</label>
                    <div class="relative">
                        <span class="absolute inset-y-0 left-0 flex items-center pl-3 text-gray-500">
                            <i class="fa fa-phone"></i>
                        </span>
                        <input type="tel" id="phone" name="phone" 
                               class="w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"
                               required>
                    </div>
                </div>

                <div>
                    <label for="address" class="block text-gray-700 mb-2">店铺地址</label>
                    <div class="relative">
                        <span class="absolute inset-y-0 left-0 flex items-center pl-3 text-gray-500">
                            <i class="fa fa-map-marker"></i>
                        </span>
                        <input type="text" id="address" name="address" 
                               class="w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500">
                    </div>
                </div>

                <div>
                    <label for="shopDesc" class="block text-gray-700 mb-2">店铺描述</label>
                    <div class="relative">
                        <span class="absolute top-3 left-3 text-gray-500">
                            <i class="fa fa-info-circle"></i>
                        </span>
                        <textarea id="shopDesc" name="shopDesc" rows="3"
                               class="w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-green-500"></textarea>
                    </div>
                </div>

                <button type="submit" class="w-full py-3 bg-green-500 text-white rounded-md hover:bg-green-600 transition">
                    <i class="fa fa-user-plus mr-2"></i>注册商家
                </button>
            </form>

            <div class="mt-6 text-center">
                <p class="text-gray-600">已有商家账号？ <a href="merchant-login.html" class="text-green-500 hover:underline">立即登录</a></p>
                <p class="mt-2"><a href="index.html" class="text-gray-500 hover:text-gray-700"><i class="fa fa-arrow-left mr-1"></i>返回首页</a></p>
            </div>

            <div id="message" class="mt-4 p-3 rounded-md hidden"></div>
        </div>
    </main>

    <footer class="bg-gray-800 text-white py-8 mt-16">
        <div class="container mx-auto px-4 text-center">
            <p>© 2025 新商城 版权所有</p>
        </div>
    </footer>

    <script>
        // 注册表单提交
        document.getElementById('registerForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const merchantData = {
                username: document.getElementById('username').value,
                password: document.getElementById('password').value,
                shopname: document.getElementById('shopname').value,
                contact_person: document.getElementById('contactPerson').value,
                phone: document.getElementById('phone').value,
                address: document.getElementById('address').value,
                shop_desc: document.getElementById('shopDesc').value,
                status: 1 // 默认状态为正常
            };
            
            try {
                const response = await fetch('http://localhost:8080/api/register/merchant', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(merchantData)
                });
                
                const result = await response.json();
                
                const messageElement = document.getElementById('message');
                messageElement.classList.remove('hidden', 'bg-green-100', 'text-green-700', 'bg-red-100', 'text-red-700');
                
                if (result.success) {
                    messageElement.classList.add('bg-green-100', 'text-green-700');
                    messageElement.textContent = '注册成功，即将跳转到商家登录页...';
                    
                    // 延迟跳转
                    setTimeout(() => {
                        window.location.href = 'merchant-login.html';
                    }, 1500);
                } else {
                    messageElement.classList.add('bg-red-100', 'text-red-700');
                    messageElement.textContent = result.message || '注册失败，请稍后重试';
                }
            } catch (error) {
                console.error('注册请求失败:', error);
                const messageElement = document.getElementById('message');
                messageElement.classList.remove('hidden', 'bg-green-100', 'text-green-700');
                messageElement.classList.add('bg-red-100', 'text-red-700');
                messageElement.textContent = '网络错误，请稍后重试';
            }
        });
    </script>
</body>
</html>